package com.saunter;
import com.alibaba.druid.pool.DruidDataSource;
import com.saunter.utils.MyInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;

@SpringBootApplication
@ServletComponentScan
@EnableTransactionManagement(proxyTargetClass = true)
//@EnableRedisHttpSession
@MapperScan({"com.saunter.mapper","com.saunter.extendMapper"})
public class SampleController extends SpringBootServletInitializer {
    @Autowired
    private MyInterceptor myInterceptor;
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // TODO Auto-generated method stub
        return builder.sources(this.getClass());
    }
    @Configuration
    class WebMvcConfigurer extends WebMvcConfigurerAdapter {
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addInterceptor(myInterceptor)
                    .addPathPatterns("/**");
        }
    }
    @Configuration
    @PropertySource(value = {"classpath:config/db.properties"}, encoding = "UTF-8")
    class DbConfig {
        @Resource
        private Environment environment;
        @Bean(name = "myDruidDataSource")
        public DataSource getDataSource() throws SQLException {
            DruidDataSource druidDataSource = new DruidDataSource();
            druidDataSource.setUrl("jdbc:mysql://127.0.0.1:"+environment.getProperty("port")+"/"+environment.getProperty("db_name")+"?serverTimezone=Asia/Shanghai&characterEncoding=utf-8");
            druidDataSource.setUsername(environment.getProperty("user_name"));
            druidDataSource.setPassword(environment.getProperty("password"));
            druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
            druidDataSource.setFilters("stat");
            druidDataSource.setMaxActive(20);
            druidDataSource.setInitialSize(1);
            druidDataSource.setMaxWait(60000);
            druidDataSource.setMinIdle(1);
            druidDataSource.setTimeBetweenEvictionRunsMillis(60000);
            druidDataSource.setMinEvictableIdleTimeMillis(300000);
            druidDataSource.setValidationQuery("select 'x'");
            druidDataSource.setTestWhileIdle(true);
            druidDataSource.setTestOnBorrow(false);
            druidDataSource.setTestOnReturn(false);
            druidDataSource.setPoolPreparedStatements(true);
            druidDataSource.setMaxOpenPreparedStatements(20);
            return druidDataSource;
        }
    }
}
